var player = {
		audio:null,
		timer:null,
		currentTime:null,
		duration:null,
		classPause:"",
		classPlay:"",
		btnPlay:null,
		btnVolume:null,
		btnMute:null,
		btnMode:null,
		volumeControl:null,
		volumeRange:null,
		modeControl:null,
		barVolume:null,
		barProcess:null,
		barDuration:null,
		playListSheet:null,
		previous:null,//上一首
		next:null,//下一首
		volume:0.6,
		isPlayed:false,//是否在播放
		playMode:1,
		playList:[],
		playedList:[],//随机播放模式下已播放曲目的存放
		currentPlayIndex:undefined,
		init :function(){
			for (var o in this){
				if(!this[o] && o != "timer" && o != "isPlayed" && o != "currentPlayIndex") {
					alert("请初始化各参数！");
					return false;
				}
			}

			var _t = this;

			// 加载播放列表
			for(var item in _t.playList){
				var _item = _t.playList[item],
					_d1 = $("<div>").addClass("itemTitle").text(_item["musicName"]),
					_d2 = $("<div>").addClass("itemSinger").text(_item["musicSinger"]),
					_d3 = $("<div>").addClass("itemDuration").text(_item["musicTime"]);

				_t.playListSheet.append($("<li>").append(_d1,_d2,_d3));
			}

			_t.playListSheet.delegate("li", "click", function(){
				if(_t.currentPlayIndex !== undefined)
					_t.playListSheet.children().eq(_t.currentPlayIndex).removeClass("activeItem");
				_t.currentPlayIndex = $(this).index();
				_t.play();
			});

			_t.audio.volume = _t.volume;
			$(_t.barVolume).height(_t.volume * ($(_t.volumeRange).height()));

			if(this.audio.duration){
				$(this.duration).text(this.calculateTime(Math.ceil(this.audio.duration)));
				$(this.currentTime).text(this.calculateTime(Math.ceil(this.audio.currentTime)));
			}
			else{
				$(this.duration).text("0:00");
				$(this.currentTime).text("0:00");
			}

			$(_t.barDuration).mouseover(function(e){
				if(!_t.audio.duration) return false;
				$(this).attr("title", _t.calculateTime(Math.ceil(_t.getX(e) / $(_t.barDuration).width() * _t.audio.duration)));
			}).click(function(e){
				if(!_t.isPlayed) return false;
				_t.audio.currentTime = Math.ceil(_t.getX(e) / $(_t.barDuration).width() * _t.audio.duration);
			});

			$(_t.btnVolume).mouseover(function(){
				$(_t.volumeControl).show();
			});

			$(_t.volumeControl).mouseout(function(e){
				//解决鼠标移到子元素上时也触发mouseout事件
				if(this.contains(e.relatedTarget || e.toElement)) return false;
				$(this).hide();
			});

			_t.btnPlay.click(function(){
				_t.isPlayed ? _t.pause() : _t.play();
			});

			/*
			*鼠标控制音量
			*/
			$(_t.volumeRange).click(function(e){
				var _this = $(this),
					height = _this.height() - e.pageY + _this.offset().top;//计算滑块高度
				$(_t.barVolume).height(height);
				_t.audio.volume = height / _this.height();
			}); 

			$(_t.btnMode).mouseover(function(){
				$(_t.modeControl).show();
			});

			$(_t.modeControl).mouseout(function(e){
				if(this.contains(e.relatedTarget || e.toElement)) return false;
				$(this).hide();
			});

			/*
			*播放模式--------------------------------------------暂不用
			*/
			_t.modeControl.delegate("div", "click", function(){
				var btnMode = _t.btnMode,
					playMode = $(this).attr("type") * 1;

				if(_t.playMode === playMode) 
					return false;//如果当前播放模式跟选中的模式一致则返回
				_t.exchangePosition(this);
				btnMode.get(0).className = "";
				_t.playMode = playMode;
				switch(playMode){
					case 1:
						btnMode.addClass("order_play_status");
						break;
					case 2:
						btnMode.addClass("random_play_status");
						break;
					case 3:
						btnMode.addClass("repeat_one_status");
						break;
					case 4:
						btnMode.addClass("repeat_all_status");
						break;
				}
				_t.modeControl.hide();
			});

			//---------------------------------------------------静音按钮
			_t.btnMute.click(function(){ 
				if(_t.audio.muted){
					$(this).removeClass("mute_bg").addClass("unmute_bg");
					$(_t.btnVolume).removeClass("btn_volume_mute").addClass("btn_volume_unmute");
					$(_t.barVolume).show();
				}
				else{
					$(this).removeClass("unmute_bg").addClass("mute_bg");
					$(_t.btnVolume).removeClass("btn_volume_unmute").addClass("btn_volume_mute");
					_t.volume = Math.ceil($(_t.barVolume).height()) / 100;
					$(_t.barVolume).hide();
				}
				_t.audio.muted = !(_t.audio.muted);
			});

			//--------------------------------------------------上一首
			_t.previous.click(function(){
				if($(this).hasClass("btn_last")){
					_t.playListSheet.children().eq(_t.currentPlayIndex).removeClass("activeItem");
					_t.audio.src = _t.playList[_t.currentPlayIndex--];
					_t.play();
				}
			});
			//--------------------------------------------------下一首
			_t.next.click(function(){
				if($(this).hasClass("btn_next")){
					_t.playListSheet.children().eq(_t.currentPlayIndex).removeClass("activeItem");
					_t.nextMusic();
				}
			});
		},
		play:function(){//播放
			var t = this,
				width = $(t.barDuration).width();
			/*
			* 当歌曲处于播放完毕下一首还没开始播放时或播放器第一次打开时
			* paused属性为true，ended属性为false，duration为NaN
			*/
			if(t.audio.paused && !t.audio.ended && !t.audio.duration){//在没有曲子播放时候
				if(t.playMode === 2){
					t.currentPlayIndex = t.getRandomIndex();
				}
				this.currentPlayIndex = this.currentPlayIndex || 0;
				t.audio.src = t.playList[this.currentPlayIndex].url;
			}
			if(!t.audio.paused && !t.audio.ended && t.audio.duration){//有曲子在播放
				t.audio.src = t.playList[this.currentPlayIndex].url;
			}
			t.audio.play();
			t.isPlayed = true;
			t.playListSheet.children().eq(t.currentPlayIndex).addClass("activeItem");
			t.btnStatus();
			t.timer = setInterval(function(){
				var duration = t.audio.duration,
					currentTime = t.audio.currentTime;
				$(t.duration).text(t.calculateTime(Math.ceil(duration)));
				duration ? $(t.barProcess).width(currentTime / duration * width) : $(t.barProcess).width(0);
				$(t.currentTime).text(t.calculateTime(Math.ceil(currentTime)));
				if(t.audio.ended){
					clearInterval(t.timer);
					t.initializeConsole();
					t.playNext();
				}
			}, 200);
		},
		pause:function(){
			this.isPlayed = false;
			this.audio.pause();
			this.btnStatus();
			clearInterval(this.timer);
		},
		extend:function(obj){
			$.extend(this, obj);
		},
		btnStatus:function(){
			if(this.currentPlayIndex > 0){
				if(!this.previous.hasClass("btn_last"))
					this.previous.removeClass("btn_last_disabled").addClass("btn_last");
			}
			else this.previous.removeClass("btn_last").addClass("btn_last_disabled");
			if(this.currentPlayIndex + 1 < this.playList.length){
				if(!this.next.hasClass("btn_next"))
					this.next.removeClass("btn_next_disabled").addClass("btn_next");
			}
			else this.next.removeClass("btn_next").addClass("btn_next_disabled");
			this.isPlayed ?
			$(this.btnPlay).attr("title", "暂停").removeClass(this.classPlay).addClass(this.classPause) :
			$(this.btnPlay).attr("title", "播放").removeClass(this.classPause).addClass(this.classPlay);
		},	
		initializeConsole:function(){
			this.isPlayed = false;
			this.btnStatus();
			$(this.currentTime).text("0:00");
			$(this.duration).text("0:00");
			$(this.barProcess).width(0);
		},
		getX:function(e){
			return e.pageX - $(this.barDuration).offset().left;
		},
		calculateTime:function(time){
			if(!time) return "0:00";
			var minute = Math.floor(time / 60),
				second = time % 60;
			return (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
		},
		exchangePosition:function(elm){
			var _this = $(elm);
			_this.parent().append(_this.remove());
		},
		playNext:function(){
			this.playListSheet.children().eq(this.currentPlayIndex).removeClass("activeItem");
			switch(this.playMode){
				case 1://顺序播放
					if(this.playList[this.currentPlayIndex + 1]){
						this.nextMusic();
					}
					break;
				case 2://随机播放
					this.currentPlayIndex = this.getRandomIndex();
					this.audio.src = this.playList[this.currentPlayIndex].url;
					this.play();
					break;
				case 3://单曲循环
					this.play();
					break;
				case 4://列表循环
					this.currentPlayIndex = ++this.currentPlayIndex % this.playList.length;
					this.audio.src = this.playList[this.currentPlayIndex].url;
					this.play();
					break;
			}
		},
		nextMusic:function(){
			this.currentPlayIndex++;
			this.audio.src = this.playList[this.currentPlayIndex].url;
			this.play();
		},
		getRandomIndex:function(){
			var rnd = Math.floor(Math.random() * this.playList.length);
			if(this.currentPlayIndex === rnd)
				return arguments.callee();
			return rnd;
		}
	};

window.onload = function(){
	//player初始化
	player.extend({
		audio:$("#audio").get(0),
		currentTime:$("#currentTime"),
		duration:$("#duration"),
		classPause:"btn_pause",
		classPlay:"btn_play",
		btnPlay:$("#btnPlay"),
		btnVolume:$("#btnVolume"),
		btnMute:$("#btnMute"),
		btnMode:$("#btnMode"),
		volumeControl:$("#volumeControl"),
		volumeRange:$("#volumeRange"),
		modeControl:$("#modeControl"),
		barVolume:$("#barVolume"),
		barProcess:$("#playProgress"),
		barDuration:$("#playDuration"),
		playListSheet:$("#playListSheet ul"),
		previous:$("#previous"),
		next:$("#next"),
		playList:[
			{
                "musicName": "天黑黑 (原唱：孙燕姿)",
                "musicSinger": "张学友",
                "url": "http://www.feelcss.com/demo/html5-player/music/thh.ogg",
                "musicTime": "03:47"
            },
            {
                "musicName": "听海 (原唱：张惠妹)",
                "musicSinger": "张学友",
                "url": "http://www.feelcss.com/demo/html5-player/music/th.ogg",
                "musicTime": "04:48"
            },
            {
                "musicName": "情人 (原唱：Beyond)",
                "musicSinger": "张学友",
                "url": "http://www.feelcss.com/demo/html5-player/music/qr.ogg",
                "musicTime": "07:52"
            },
            {
                "musicName": "爱我别走 (原唱：张震岳)",
                "musicSinger": "张学友",
                "url": "http://www.feelcss.com/demo/html5-player/music/awbz.ogg",
                "musicTime": "05:15"
            },
            {
                "musicName": "梦见铁达尼",
                "musicSinger": "张惠妹",
                "url": "http://www.feelcss.com/demo/html5-player/music/mjtdn.ogg",
                "musicTime": "04:37"
            },
            {
                "musicName": "很难",
                "musicSinger": "张震岳",
                "url": "http://www.feelcss.com/demo/html5-player/music/hn.ogg",
                "musicTime": "04:18"
            },{
                "musicName": "There You'll Be",
                "musicSinger": "Faith Hill",
                "url": "http://www.feelcss.com/demo/html5-player/music/There_You'll_Be.ogg",
                "musicTime": "03:41"
            },
            // {
            //     "musicName": "Tennessee",
            //     "musicSinger": "Faith Hill",
            //     "url": "http://www.feelcss.com/demo/html5-player/music/tennessee.ogg",
            //     "musicTime": "03:40"
            // },
            {
                "musicName": "Brothers",
                "musicSinger": "Faith Hill",
                "url": "http://www.feelcss.com/demo/html5-player/music/Brothers.ogg",
                "musicTime": "04:04"
            },
            {
                "musicName": "And Then I Kissed Him",
                "musicSinger": "Faith Hill",
                "url": "http://www.feelcss.com/demo/html5-player/music/And_Then_I_Kissed_Him.ogg",
                "musicTime": "05:36"
            },
            {
                "musicName": "Maria",
                "musicSinger": "Faith Hill",
                "url": "http://www.feelcss.com/demo/html5-player/music/Maria.ogg",
                "musicTime": "03:08"
            },
            {
                "musicName": "Vincent",
                "musicSinger": "roberto vecchioni",
                "url": "http://www.feelcss.com/demo/html5-player/music/Vincent.ogg",
                "musicTime": "02:07"
            }
		]
	});
	player.init();
}


// 工具集
function log(msg){
	return console.log(msg);
}

